<?php
/**
 * 商家结算
 * User: ws
 * Date: 2018/10/13
 * Time: 10:22
 */

namespace app\merchant\controller;
use think\Db;

class Sett  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['order_sn|phone|name|consignee'] = array('like','%'.trim($key).'%');
            $where['order_status'] = 3;
            $where['store_id'] = $_SESSION['think']['admin_user']['store_id'];
            $where['settlement'] = 0;

            $list = Db::table('je_order')->field('order_id,order_sn,store_id,add_time,end_time,confirm_time,type,lease_time,lease_end_time,abrasion_price,overdue_price,settlement,business_settlement_price')->order('order_id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$val){
                $list['data'][$key]['lease_end_time'] = floor(((strtotime(date('Y-m-d',$val['lease_end_time']))+86400)-strtotime(date('Y-m-d',$val['lease_time'])))/86400);
                $list['data'][$key]['goods'] = Db::table('je_order_goods')->alias('o')->join('je_goods g','o.goods_id = g.goods_id')->join('je_spec_goods_price s','o.goods_id = s.goods_id and o.spec_item = s.price_key')->field('o.rent,o.settlement,o.goods_price,o.goods_number,o.spec_item_key,g.name as gname,s.settlement_price')->where("order_id = {$val['order_id']}")->select();
            }

            //先求出结算价格
            foreach($list['data'] as $k=>$item){
                foreach($item['goods'] as $ke=>$it){
                    $list['data'][$k]['s_price'] = 0;
                    if($item['type']==1){
                        $list['data'][$k]['s_price'] = ($item['lease_end_time'] * $item['business_settlement_price'])+$item['abrasion_price'];
                    }else if($item['type']==2){
                        $list['data'][$k]['s_price'] = floatval($item['business_settlement_price']);

                    }
                }

            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function settlement(){
        if(request()->isPost()) {
            $data = $this->request->post();
            $data['orderid'] = implode(',',$data['id']);
            $data['settlement_sn'] = 'J'.generateOrderNum();
            $data['add_time'] = time();
            $data['store_id'] = $_SESSION['think']['admin_user']['store_id'];

            foreach($data['id'] as $k=>$item){
                $list[$k]['order_id'] = 0;
                $list[$k]['settlement'] = 1;
                $list[$k]['order_id'] = $item;
            }

            unset($data['id']);

            Db::startTrans();
            try{
                Db::name('settlement')->insertGetId($data);
                Db::commit();
                $this->order = model('admin/order');
                $this->order->saveAll($list);
                return json(array('code'=>200,'msg'=>'操作成功，结算订单已提交到平台'));
            } catch (\Exception $e) {
                Db::rollback();
                return json(array('code'=>0,'msg'=>$e->getMessage()));
            }

        }
    }

    function index2(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            if(is_numeric(input('type')) === true){
                $where['pay_state'] = input('type');
            }
            $where['t.settlement_sn|t.fees'] = array('like','%'.trim($key).'%');
            $where['t.store_id'] = $_SESSION['think']['admin_user']['store_id'];
            $list = Db::table('je_settlement')->alias('t')->join('je_store s','t.store_id = s.store_id')->field('s.name,t.*')->order('settlement_id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$val){
                $list['data'][$key]['orderid'] = count(explode(',',$val['orderid']));
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list2');
    }
}